﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmManageAccounts : Form
    {
        Form opener;
        List<Person> employees;

        public frmManageAccounts(Form Opener)
        {
            opener = Opener;
            InitializeComponent();
        }

        void refresh()
        {
            employees = Person.loadAllNurses();

            dgvAccounts.Rows.Clear();
            foreach (Person item in employees)
            {
                String skills = "";
                foreach (int skill in item.SkillTableIDs)
	            {
                    skills += Skill.loadData(skill).Name + " ";
	            }

                String department = "";
                if (item.DepartmentTableIDs.Count > 0)
                {
                    department = Department.loadData(item.DepartmentTableIDs[0]).Name;
                }

                String title = "";
                if (item.TitleTableIDs.Count > 0)
                {
                    title = Title.loadData(item.TitleTableIDs[0]).Name;
                }                

                dgvAccounts.Rows.Add(item.FullName, item.IsManager, PayGrade.loadData(item.PayGradeTableID).Name, 
                    department, title, Person.loadPerson(item.SuperiorID).FullName, skills, item.PERID);
            }
        }

        private void btnAddAccount_Click(object sender, EventArgs e)
        {
            frmManageAccount add = new frmManageAccount(this);

            this.Hide();
            add.Show();
        }

        private void btnEditAccount_Click(object sender, EventArgs e)
        {
            if (dgvAccounts.SelectedRows.Count > 0)
            {
                Person person = new Person();
                foreach (Person item in employees)
                {
                    //PERID is unique, so use that instead of Fullname
                    if (item.PERID == (int)dgvAccounts.SelectedRows[0].Cells[7].Value)
                    {
                        person = item;
                    }
                }

                frmManageAccount edit = new frmManageAccount(this, person);

                this.Hide();
                edit.Show();
            }
        }

        private void btnDeleteAccount_Click(object sender, EventArgs e)
        {
            if (dgvAccounts.SelectedRows.Count > 0)
            {
                Person person = new Person();
                foreach (Person item in employees)
                {
                    //PERID is unique, so use that instead of Fullname
                    if (item.PERID == (int)dgvAccounts.SelectedRows[0].Cells[7].Value)
                    {
                        person = item;
                    }
                }

                if (person != null)
                {
                    if (person.removeData() == 0)
                    {
                        refresh();
                    }
                    else
                    {
                        MessageBox.Show("Problem deleting selected account!");
                    }
                }
            }
        }

        private void btnBack(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmManageAccounts_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true)
                refresh();
        }

        private void frmManageAccounts_FormClosing(object sender, FormClosingEventArgs e)
        {
            opener.Show();
        }
    }
}
